package com.itheima.filter;

import com.itheima.common.JwtUtils;
import org.springframework.context.annotation.ComponentScan;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class LoginCheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String uri = request.getRequestURI();
        if (uri.equals("/login")){
        filterChain.doFilter(servletRequest,servletResponse);
        return;
        }
        String token = request.getHeader("token");
        try {
            JwtUtils.parseJWT(token);
            filterChain.doFilter(servletRequest, servletResponse);
        }catch (Exception e){
            response.setStatus(401);
        }
    }

    }
